package com.amsp.oa.oademo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.amsp.oa.oademo.entity.EmployeeFile;
import com.amsp.oa.oademo.entity.EmployeeEducation;
import com.amsp.oa.oademo.entity.EmployeeWork;
import com.amsp.oa.oademo.entity.EmployeeFamily;
import com.amsp.oa.oademo.mapper.EmployeeFileMapper;
import com.amsp.oa.oademo.mapper.EmployeeEducationMapper;
import com.amsp.oa.oademo.mapper.EmployeeWorkMapper;
import com.amsp.oa.oademo.mapper.EmployeeFamilyMapper;
import com.amsp.oa.oademo.service.EmployeeFileService;
import com.amsp.oa.oademo.vo.EmployeeFileVO;
import com.amsp.oa.oademo.vo.EmployeeEducationVO;
import com.amsp.oa.oademo.vo.EmployeeWorkVO;
import com.amsp.oa.oademo.vo.EmployeeFamilyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmployeeFileServiceImpl extends ServiceImpl<EmployeeFileMapper, EmployeeFile>
        implements EmployeeFileService {

    @Resource
    private EmployeeEducationMapper educationMapper;

    @Resource
    private EmployeeWorkMapper workMapper;

    @Resource
    private EmployeeFamilyMapper familyMapper;

    @Override
    public IPage<EmployeeFile> getPage(Integer current, Integer size, String keyword) {
        Page<EmployeeFile> page = new Page<>(current, size);
        LambdaQueryWrapper<EmployeeFile> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(keyword)) {
            wrapper.like(EmployeeFile::getName, keyword)
                    .or()
                    .like(EmployeeFile::getApplicationPosition, keyword);
        }

        wrapper.orderByDesc(EmployeeFile::getCreateTime);
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public EmployeeFileVO getDetail(Long id) {
        EmployeeFile employeeFile = getById(id);
        if (employeeFile == null) {
            return null;
        }

        EmployeeFileVO vo = new EmployeeFileVO();
        BeanUtils.copyProperties(employeeFile, vo);

        // 查询教育经历
        LambdaQueryWrapper<EmployeeEducation> eduWrapper = new LambdaQueryWrapper<>();
        eduWrapper.eq(EmployeeEducation::getEmployeeId, id);
        List<EmployeeEducation> educations = educationMapper.selectList(eduWrapper);
        List<EmployeeEducationVO> educationVOs = educations.stream().map(edu -> {
            EmployeeEducationVO eduVO = new EmployeeEducationVO();
            BeanUtils.copyProperties(edu, eduVO);
            return eduVO;
        }).collect(Collectors.toList());
        vo.setEducations(educationVOs);

        // 查询工作经历
        LambdaQueryWrapper<EmployeeWork> workWrapper = new LambdaQueryWrapper<>();
        workWrapper.eq(EmployeeWork::getEmployeeId, id);
        List<EmployeeWork> works = workMapper.selectList(workWrapper);
        List<EmployeeWorkVO> workVOs = works.stream().map(work -> {
            EmployeeWorkVO workVO = new EmployeeWorkVO();
            BeanUtils.copyProperties(work, workVO);
            return workVO;
        }).collect(Collectors.toList());
        vo.setWorks(workVOs);

        // 查询家庭成员
        LambdaQueryWrapper<EmployeeFamily> familyWrapper = new LambdaQueryWrapper<>();
        familyWrapper.eq(EmployeeFamily::getEmployeeId, id);
        List<EmployeeFamily> families = familyMapper.selectList(familyWrapper);
        List<EmployeeFamilyVO> familyVOs = families.stream().map(family -> {
            EmployeeFamilyVO familyVO = new EmployeeFamilyVO();
            BeanUtils.copyProperties(family, familyVO);
            return familyVO;
        }).collect(Collectors.toList());
        vo.setFamilies(familyVOs);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateEmployee(EmployeeFileVO vo) {
        // 保存或更新主表
        EmployeeFile employeeFile = new EmployeeFile();
        BeanUtils.copyProperties(vo, employeeFile);
        saveOrUpdate(employeeFile);

        Long employeeId = employeeFile.getId();

        // 处理教育经历
        if (vo.getEducations() != null) {
            // 删除原有记录
            LambdaQueryWrapper<EmployeeEducation> eduWrapper = new LambdaQueryWrapper<>();
            eduWrapper.eq(EmployeeEducation::getEmployeeId, employeeId);
            educationMapper.delete(eduWrapper);

            // 保存新记录
            List<EmployeeEducation> educations = vo.getEducations().stream().map(eduVO -> {
                EmployeeEducation education = new EmployeeEducation();
                BeanUtils.copyProperties(eduVO, education);
                education.setEmployeeId(employeeId);
                return education;
            }).collect(Collectors.toList());
            educations.forEach(education -> educationMapper.insert(education));
        }

        // 处理工作经历
        if (vo.getWorks() != null) {
            // 删除原有记录
            LambdaQueryWrapper<EmployeeWork> workWrapper = new LambdaQueryWrapper<>();
            workWrapper.eq(EmployeeWork::getEmployeeId, employeeId);
            workMapper.delete(workWrapper);

            // 保存新记录
            List<EmployeeWork> works = vo.getWorks().stream().map(workVO -> {
                EmployeeWork work = new EmployeeWork();
                BeanUtils.copyProperties(workVO, work);
                work.setEmployeeId(employeeId);
                return work;
            }).collect(Collectors.toList());
            works.forEach(work -> workMapper.insert(work));
        }

        // 处理家庭成员
        if (vo.getFamilies() != null) {
            // 删除原有记录
            LambdaQueryWrapper<EmployeeFamily> familyWrapper = new LambdaQueryWrapper<>();
            familyWrapper.eq(EmployeeFamily::getEmployeeId, employeeId);
            familyMapper.delete(familyWrapper);

            // 保存新记录
            List<EmployeeFamily> families = vo.getFamilies().stream().map(familyVO -> {
                EmployeeFamily family = new EmployeeFamily();
                BeanUtils.copyProperties(familyVO, family);
                family.setEmployeeId(employeeId);
                return family;
            }).collect(Collectors.toList());
            families.forEach(family -> familyMapper.insert(family));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBatch(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);

        // 删除主表记录
        removeByIds(idList);

        // 删除教育经历
        LambdaQueryWrapper<EmployeeEducation> eduWrapper = new LambdaQueryWrapper<>();
        eduWrapper.in(EmployeeEducation::getEmployeeId, idList);
        educationMapper.delete(eduWrapper);

        // 删除工作经历
        LambdaQueryWrapper<EmployeeWork> workWrapper = new LambdaQueryWrapper<>();
        workWrapper.in(EmployeeWork::getEmployeeId, idList);
        workMapper.delete(workWrapper);

        // 删除家庭成员
        LambdaQueryWrapper<EmployeeFamily> familyWrapper = new LambdaQueryWrapper<>();
        familyWrapper.in(EmployeeFamily::getEmployeeId, idList);
        familyMapper.delete(familyWrapper);
    }
}